package com.liuwang.meal.ui;

import com.api.util.OnlinePay;
import com.liuwang.meal.R;
import com.liuwang.meal.manager.OrderDetailManager;
import com.liuwang.meal.util.SharedPrefer;

import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class adminPayActivity extends Activity {

	private EditText et_username, et_password;
	String username, password, money;

	ProgressDialog progress;
	private Integer type_id ;

	private Handler handler = new Handler() {
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 1:
				String info = (String) msg.obj;
				String[] infoArrey = info.split("@");
				
				if (infoArrey[0].equals("000")){
					Toast.makeText(getApplicationContext(), "密码错误", 0).show();
				}
				
				else if (infoArrey[0].equals("11")){
//					new OrderDetailManager(adminPayActivity.this).updateState(1, 5);
//					Toast.makeText(getApplicationContext(), "结账成功", 1).show();
//					finish();	
//					Intent intent = new Intent(adminPayActivity.this, OrderActivity.class);
//					intent.putExtra("type_id", type_id);
//					startActivityForResult(intent, 0);
					Toast.makeText(getApplicationContext(), "结账成功", 1).show();
					new OrderDetailManager(adminPayActivity.this).updateState(1, 5);
					OrderDetailManager dd = new OrderDetailManager(getApplicationContext());
					dd.truncate();				
					Intent intent = new Intent(adminPayActivity.this, MenuActivity.class);
					intent.putExtra("type_id", type_id);
					startActivityForResult(intent, 0);
					finish();
					
//					Intent intent = new Intent(PayActivity.this, OrderActivity.class);
//					startActivityForResult(intent, 0);
				}

					break;
			case 0:
				Toast.makeText(getApplicationContext(), "连接网络失败,请检查网络是否连接", 0)
						.show();
				break;
			}
		}
	};
	 
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.pay_activity);

		et_username = (EditText) findViewById(R.id.et_username);
		et_password = (EditText) findViewById(R.id.et_password);
		et_username.setVisibility(View.GONE);

		Intent intent = getIntent();
		 
		type_id = intent.getExtras().getInt("type_id");
		 
		
		

	}

	 

	public void pay(View view) {

//		username = et_username.getText().toString();
		password = et_password.getText().toString();
		new Thread(new Runnable() {
			@Override
			public void run() {
				try {

					String log = OnlinePay.adminPay(getApplicationContext(),
							"1", password);
					Message msg = new Message();
					msg.what = 1;
					msg.obj = log;
					handler.sendMessage(msg);
				} catch (Exception e) {
					e.printStackTrace();
					Message msg = new Message();
					msg.what = 0;
					handler.sendMessage(msg);
				}

			}
		}).start();

	}

	 

}
